/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DataAccess;

import BussinesLogic.Request;
import DataModel.*;
import java.sql.*;

public class Dao_Disciplina {

    public Dao_Disciplina(){}
    
    public Request registrarDisciplina(Disciplina disciplina){
        
        String sql = "INSERT INTO \"Disciplina\" (nombre, descripcion, logo)"
                + " VALUES('" + disciplina.getNombre() + "','" + disciplina.getDescripcion()
                + "','" + disciplina.getLogo() + "')";
        
        Request request = new Request();

        try {
            Connect conn = new Connect();
            driverPGSQL driver = new driverPGSQL(conn);
            request = driver.queryInsert(sql);
            conn.close();
        } catch (Exception e) {
            request.setTransac(false);
            request.setMessage(e.getMessage());
        }
        return request;
    }
    
    public Request registrarDisciplinaEvento(String evento, String disciplina){
        
        String sql = "INSERT INTO \"DisciplinasEvento\" (id_evento, disciplina)"
                + " VALUES('" + evento + "','" + disciplina + "')";
        
        Request request = new Request();

        try {
            Connect conn = new Connect();
            driverPGSQL driver = new driverPGSQL(conn);
            request = driver.queryInsert(sql);
            conn.close();
        } catch (Exception e) {
            request.setTransac(false);
            request.setMessage(e.getMessage());
        }
        return request;
    }
    
    public Request eliminarDisciplina(Disciplina disciplina){
        
        String sql = "DELETE FROM \"Disciplina\" WHERE nombre='" + disciplina.getNombre() + "'";
        Request request = new Request();
        
        try{
            Connect conn = new Connect();
            driverPGSQL driver = new driverPGSQL(conn);
            request = driver.queryDelete(sql);
            conn.close();
        }
        catch(Exception e){
            request.setTransac(false);
            request.setMessage(e.getMessage());
        }
        
        return request;
    }
    
    public Request consultarDisciplinaPorEvento(String evento) {
        
        Request request = new Request();
        String sql = "SELECT * FROM \"Disciplina\" t1,\"DisciplinasEvento\" t2 "
                + "WHERE t2.id_evento='" + evento + "' AND t1.nombre=t2.disciplina";        
        
        java.util.Vector<Disciplina> v = new java.util.Vector(1,1);
        try {
            Connect conn = new Connect();
            driverPGSQL driver = new driverPGSQL(conn);
            request = driver.querySelect(sql);
            ResultSet rqs = request.getRequest();
            while (rqs.next()) {
                Disciplina disc = new Disciplina();
                disc.setNombre(rqs.getString(1));
                disc.setDescripcion(rqs.getString(2));
                disc.setLogo(rqs.getString(3));
                v.add(disc);
            }
            request.setObject(v);
            conn.close();
        } catch (Exception e) {
            request.setTransac(false);
            request.setMessage(e.getMessage());
        }
        
        return request;
    }
    
    
    
}
